package com.itheima.handler;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 包名:com.itheima.handler
 * @author Leevi
 * 日期2020-08-15  10:30
 * 1. 自定义一个异常处理器类，实现HandlerExceptionResolver接口
 * 2. 重写resolveException方法，对异常进行统一处理
 * 3. 在springmvc的配置文件中，配置异常处理器
 */
public class GlobalExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",ex.getMessage());
        //打印异常信息
        ex.printStackTrace();
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
